package com.example.cool_core.config;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.cool_core.R;
import com.example.cool_core.listener.OnConfigListener;
import com.example.cool_core.listener.OnErrorOfLoadingView;
import com.example.cool_core.listener.OnTopBarLeftListener;


/**
 * 1 没有设置loading 和 error 的视图
 * <p>
 * 2 设置了
 * <p>
 * Created by liudong on 2017/11/28.
 */

public class PublicConfig {
    private View layoutContent, layoutLoading, layoutError;
    //content根视图容器
    private FrameLayout mLayoutBootView;
    //topBar容器
    private FrameLayout mLayoutTopBarView;
    //用于设置视图 方便而已
    private LayoutInflater mLayoutInflater;
    private Activity mActivity;
    /**
     * 配置的监听
     */
    private OnConfigListener onConfigListener;

    private TextView mTvTopBarTitle;
    //是否已经有数据了
    private boolean isHttpSuccess;

    public PublicConfig(Activity activity, LayoutInflater inflater, OnConfigListener onConfigListener, OnErrorOfLoadingView onErrorOfLoadingView) {
        this.mActivity = activity;
        this.onConfigListener = onConfigListener;
        this.mLayoutInflater = inflater;
        this.mLayoutBootView = onErrorOfLoadingView.getBootView();
        this.mLayoutTopBarView = onErrorOfLoadingView.getTopbarView();
        isHttpSuccess = false;
        ViewGroup loadingRootLayout = onErrorOfLoadingView.getLoadingLayouView();
        //设置loading模块 如果没设置 默认contentRoot
        if (loadingRootLayout == null) {
            loadingRootLayout = mLayoutBootView;
        }
        if (onErrorOfLoadingView.isLoadingOrErrorView()) {
            setLoadOrErrorRootLayout(loadingRootLayout);
        }
    }

    /**
     * 给定的的viewGroup中添加 loadingView 和 errorView
     * 并获取contentView 就是主要显示的内容View
     */
    public PublicConfig setLoadOrErrorRootLayout(ViewGroup loadOrErrorRootViewGroup) {
        int childCount = loadOrErrorRootViewGroup.getChildCount();
        if (childCount != 1) {
            throw new RuntimeException("loadOrErrorRootViewGroup 的子view只能是一个");
        }
        //获取内容view 和 添加 loadView errorView
        layoutContent = loadOrErrorRootViewGroup.getChildAt(0);
        View loadingView, errorView;
        if (CoolViewConfig.getInstance().getLoadingLayout() == 0) {
            loadingView = mLayoutInflater.inflate(R.layout.cool_loading, loadOrErrorRootViewGroup);
        } else {
            loadingView = mLayoutInflater.inflate(CoolViewConfig.getInstance().getLoadingLayout(), loadOrErrorRootViewGroup);
        }
        if (CoolViewConfig.getInstance().getErrorLayout() == 0) {
            errorView = mLayoutInflater.inflate(R.layout.cool_error, loadOrErrorRootViewGroup);
        } else {
            errorView = mLayoutInflater.inflate(CoolViewConfig.getInstance().getErrorLayout(), loadOrErrorRootViewGroup);
        }
        layoutLoading = loadingView.findViewById(R.id.cool_layout_loading);
        layoutError = errorView.findViewById(R.id.cool_layout_error);
        /**
         * 初始化errorView里面的方法
         */
        View mTvErrorBtn = layoutError.findViewById(R.id.cool_refresh);
        mTvErrorBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showLoading();
                onConfigListener.configHttpRefresh();
            }
        });
        //默认显示内容
        showContent();
        return this;
    }


    /**
     * 设置topbar
     * 默认带有阴影效果
     *
     * @param name                 topbar的标题
     * @param isShowLife           是否显示左边
     * @param onTopBarLeftListener 监听
     */
    public PublicConfig setTopBar(String name, boolean isShowLife, final OnTopBarLeftListener onTopBarLeftListener) {
        //有topbar
        View topBarView;
        if (CoolViewConfig.getInstance().getTopBarLayout() == 0) {
            topBarView = mLayoutInflater.inflate(R.layout.cool_topbar, mLayoutTopBarView);
        } else {
            topBarView = mLayoutInflater.inflate(CoolViewConfig.getInstance().getTopBarLayout(), mLayoutTopBarView);
        }
        LinearLayout liftLayout = topBarView.findViewById(R.id.cool_layout_left);
        liftLayout.setVisibility(View.VISIBLE);
        if (!isShowLife) {
            liftLayout.setVisibility(View.GONE);
        }
        mTvTopBarTitle = topBarView.findViewById(R.id.cool_tv_title);
        mTvTopBarTitle.setText(name);
        //设置点击事件
        liftLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onTopBarLeftListener != null) {
                    onTopBarLeftListener.onClick();
                } else {
                    mActivity.finish();
                }
            }
        });
        return this;
    }

    public PublicConfig setTopBar(String name) {
        if (mTvTopBarTitle != null) {
            mTvTopBarTitle.setText(name);
        } else {
            setTopBar(name, true, null);
        }
        return this;
    }

    //只显示loading
    public void showLoading() {
        //有数据后加载就不显示loading了
        //没有数据就显示
        if (!isHttpSuccess) {
            if (layoutContent != null) {
                layoutContent.setVisibility(View.GONE);
            }
            if (layoutLoading != null) {
                layoutLoading.setVisibility(View.VISIBLE);
            }
            if (layoutError != null) {
                layoutError.setVisibility(View.GONE);
            }
        }
    }

    //只显示ErrorView
    public void showError() {
        //加载失败 关闭所有的刷新
        onConfigListener.finishAllRefresh(false);
        if (!isHttpSuccess) {
            //有数据后就不显示ErrorView
            //没有数据就显示
            if (layoutContent != null) {
                layoutContent.setVisibility(View.GONE);
            }
            if (layoutLoading != null) {
                layoutLoading.setVisibility(View.GONE);
            }
            if (layoutError != null) {
                layoutError.setVisibility(View.VISIBLE);
            }
        }
    }

    //显示 contentView
    public void showContent() {
        //加载成功 关闭下拉刷新 加载更多会再baseActivity 中关闭
        onConfigListener.finishRefresh(true);
        if (layoutContent != null) {
            layoutContent.setVisibility(View.VISIBLE);
        }
        if (layoutLoading != null) {
            layoutLoading.setVisibility(View.GONE);
        }
        if (layoutError != null) {
            layoutError.setVisibility(View.GONE);
        }
    }


    public void setHttpSuccess(boolean httpSuccess) {
        isHttpSuccess = httpSuccess;
        onConfigListener.setIsHttpSuccess(httpSuccess);
    }


    public boolean isHttpSuccess() {
        return isHttpSuccess;
    }


}
